import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
			uploadNum: 0,
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
			modelShow: false,
			ismodelShow: false,
			friendlyLinkList: [], //友情连接列表
			portalConfigList: [], //门户配置列表
			validate: {}, //验证对象(友情连接)
			validate2: {}, //验证对象(其他连接)
			portalConfig: {}, //门户配置信息
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		//初始化友情连接列表分页信息
		this.searchFriendlyLinkList();
		//初始化门户配置列表分页信息
		this.searchPortalConfigList();
	},
	mounted() {
		let that = this;

		//校验名称重复
		$.validator.addMethod("checkNameRepeat", function(value, element, params) {
			let flg = false;
			if(that.portalConfig.id != null && (value == that.portalConfig.name)) {
				flg = true;
			} else {
				$.ajax({
					type: "POST",
					async: false,
					url: that.urls.checkPortalConfigName,
					data: JSON.stringify({
						"name": value
					}),
					dataType: "json",
					contentType: "application/json",
					success: function(data) {
						if(data.success) {
							flg = !data.data;
						}
					}
				});
			}
			return this.optional(element) || flg;
		}, "该链接名称已存在！");

		//加载表单验证
		this.validate = $("#portalConfigForm").validate({
			ignore: [],
			rules: {
				name: { // 连接名称
					required: true,
					maxlength: 20,
					checkNameRepeat: true
				},
				url: { // 连接地址
					required: true,
					maxlength: 200
				}
			},
			messages: {
				name: { // 连接名称
					required: "请输入链接名称",
					maxlength: "请输入长度小于20"
				},
				url: { // 连接地址
					required: "请输入URL地址",
					maxlength: "请输入长度小于50"
				}
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});

		this.validate2 = $("#portalConfigForm2").validate({
			ignore: [],
			rules: {
				name: { // 连接名称
					required: true,
					maxlength: 20
				},
				url: { // 连接地址
					required: true,
					maxlength: 100
				}
			},
			messages: {
				name: { // 连接名称
					required: "请输入链接名称",
					maxlength: "请输入长度小于20"
				},
				url: { // 连接地址
					required: "请输入URL地址",
					maxlength: "请输入长度小于100"
				}
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});
	},
	methods: { //写函数方法
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
			this.searchFriendlyLinkList()
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		openModal: function(id) {
			if (id == null && this.total >= 10) {
        layer.msg("最多只能新增10条链接数据内容!", {
          time: 3000, //3s后自动关闭
        });
			  return;
      }
			this.modelShow = true;
			this.validate.resetForm();
      this.portalConfig = {};
			//这个地方可以调用接口，也可以获取当前列表信息，具体待定
			if(id != null) { //如果有id，则查询该
        this.initPortalConfig(id);
			}
		},
		isopenModal: function(id) {
			let that = this;
			this.ismodelShow = true;
			that.portalConfig = {};
			//这个地方可以调用接口，也可以获取当前列表信息，具体待定
			if(id != null) { //如果有id，则查询该
				that.initPortalConfig(id);
			}
		},
		closeModel: function() {
			this.modelShow = false;
			this.ismodelShow = false;
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		idFrontImg: function(event) {
			var that = this;
			var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
			let formData = new FormData();
			formData.append('fileNames', file);
			let config = {
				headers: {
					'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
				}
			};
			that.$post(
					that.urls.upload,
					formData,
					config
				)
				.then(function(res) {
					if(res.success) {
						let fileInfo = res.data[0];
						$(".showHandIdFront").attr("src", that.urls.baseUrl + fileInfo.url);
						$(".handIdFrontA").css("display", "none");
						$("input[name='url']").val(fileInfo.url);
					}
				})

		},
		//初始化对象
		initPortalConfig: function(id) {
			let that = this;
			that.$post(
					that.urls.getPortalConfig, {
						"id": id
					}
				)
				.then(function(res) {
					if(res.success) {
						that.portalConfig = res.data;
					}
				})
		},
		//添加新连接
		addlinks: function() {
			let that = this;
			let flg = that.validate.form();
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.addPortalConfig,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							console.log(res);
							that.modelShow = false;
							msg = "添加成功！";
							that.searchFriendlyLinkList();
						} else {
							msg = "添加失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		//修改连接
		updateData: function(type) {
			let that = this;
			let flg = false;
			if(type == 0) {
				flg = that.validate2.form();
			} else if(type == 1) {
				flg = that.validate.form();
			}
			if(flg) {
				let dataObj = {};
				if(type == 0) {
					dataObj = that.combinationFormObject(that.validate2.currentForm);
				} else {
					dataObj = that.combinationFormObject(that.validate.currentForm);
				}
				that.$post(
						that.urls.updatePortalConfig,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							if(type == 1) {
								//重新渲染数据
								that.searchFriendlyLinkList();
							} else {
								//重新渲染数据
								that.searchPortalConfigList();
							}
              msg = "修改成功！";
              that.closeModel();
						} else {
							msg = "修改失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		//删除连接
		deleteData: function(id, type) {
			let that = this;
			layer.confirm('你确定要删除吗?', {icon: 3, title:'提示'}, function(index){
				if(id != null && type != null) {
					that.$post(
							that.urls.deletePortalConfig, {
								"id": id
							}
						)
						.then(function(res) {
							if(res.success) {
								layer.msg('删除成功！', {
									time: 3000, //3s后自动关闭
								});
								that.searchFriendlyLinkList();
							} else {
								layer.msg('删除失败！', {
									time: 3000, //3s后自动关闭
								});
							}
						})
					if(type == 0) { //渲染门户基础信息
						that.searchPortalConfigList();
					} else if(type == 1) { //渲染门户友情信息
						that.searchFriendlyLinkList();
					}
				}
			});
		},
		//友情链接列表
		searchFriendlyLinkList: function() {
			let that = this;
			that.$post(
					that.urls.getPortalConfigPageInfo, {
						"pageNum": that.page,
						"pageSize": that.size,
						"type": 1
					}
				)
				.then(function(res) {
					if(res.success) {
						console.log("友情链接列表：" + res);
						that.friendlyLinkList = res.pageInfo.list;
						that.total = res.pageInfo.total;
						that.page = res.pageInfo.pageNum;
					} else {
						layer.alert(res.message);
					}
				})
		},
		//门户配置列表
		searchPortalConfigList: function() {
			let that = this;
			that.$post(
					that.urls.getPortalConfigPageInfo, {
						"pageNum": 0,
						"pageSize": 2,
						"type": 0
					}
				)
				.then(function(res) {
					if(res.success) {
						that.portalConfigList = res.pageInfo.list;
					} else {
						layer.alert(res.message);
					}
				})
		}
	}
}
